import BaseView from "../../../cfw/mvc/BaseView";
import SnowManManager from "../SnowManManager";
import SnowToyModel from "../model/SnowToyModel";
import SnowManController from "../SnowManController";
import CollisionHelper from "../../../cfw/collide/CollisionHelper";
import ModuleManager from "../../../cfw/module/ModuleManager";
import { ResType } from "../../../cfw/res/ResInterface";
import ResItem from "../../../cfw/res/ResItem";
import SnowToyView from "./SnowToyView";
import SDKManager from "../../../sdk/SDKManager";
import { ResultState, SDKEventName } from "../../../sdk/SDKConfig";
import { ItemState } from "../../../cfw/tools/Define";

const { ccclass, property } = cc._decorator;

@ccclass
export default class SnowRoomView extends BaseView {

    @property([cc.Node])
    toyPos: cc.Node[] = []

    @property([cc.Node])
    collidePos: cc.Node[] = []


    @property([cc.Sprite])
    toySprites: cc.Sprite[] = []

    @property(cc.Prefab)
    itemPrefab: cc.Prefab = null;

    @property(cc.Node)
    root: cc.Node = null;

    private curIndex: number = -1;

    // private itemList: SnowToyView[] = []

    // private curItem: SnowToyView;

    protected model: SnowManManager;
    protected controller: SnowManController;

    protected initPos: cc.Vec3 = cc.v3()
    protected comps: SnowToyView[] = []
    start() {
        let toyList = this.model.getToyList();
        // cc.log(' toyList ', toyList.length)
        for (let index = 0; index < toyList.length; index++) {
            const element: SnowToyModel = toyList[index];
            cc.log(' element icon == ', element.getIcon())
            let node = cc.instantiate(this.itemPrefab);
            let comp: SnowToyView = this.controller.initView(node, 'SnowToyView', element) as SnowToyView;
            this.toyPos[index].addChild(node)
            this.comps[index] = comp
            // this.itemList[index] = comp;
        }


        for (let index = 0; index < this.toySprites.length; index++) {
            const element: SnowToyModel = SnowManManager.instance().getUseToy(index)
            if (element) {
                ModuleManager.loadRes(element.getIcon(), ResType.SpriteFrame, (err, res: ResItem) => {
                    if (err || !cc.isValid(this.node)) {
                        return;
                    }
                    this.toySprites[index].spriteFrame = res.getRes();
                })
            }
        }

        this.node.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this)
        this.node.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this)
        this.node.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this)
        this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.onTouchEnd, this)
        SDKManager.getChannel().sendEvent(SDKEventName.entersnow,0)
    }

    onTouchStart(e: cc.Touch) {

        let pos = e.getLocation()
        // cc.log(' onTouchStart pos.x ', pos.x, pos.y)
        // pos = this.node.convertToNodeSpaceAR(pos)
        for (let index = 0; index < this.comps.length; index++) {
            const element: SnowToyView = this.comps[index];
            // cc.log('  element.node.getBoundingBox()', element.getBoundingBoxToWorld())
            if (CollisionHelper.isPositionInRect(pos, element.getBoundingBoxToWorld())) {
                if (element.needBuy()) {
                    this.controller.openItem(element.getModel())
                    // SDKManager.getChannel().showRewardAd(0, (s) => {
                    //     if (cc.isValid(this.node))
                    //         if (s == ResultState.YES) {
                    //             element.setState(ItemState.GOT)
                    //         }
                    // })
                } else {
                    // this.curItem = element;
                    this.initPos.x = this.toyPos[index].x;
                    this.initPos.y = this.toyPos[index].y;
                    this.curIndex = index;
                    cc.log(' 点中 物体')
                }

                break;
            } else {
                cc.log(' 未点中 ')
            }
        }
    }
    onTouchMove(e: cc.Touch) {

        // pos = this.root.convertToWorldSpaceAR(pos)
        if (this.curIndex >= 0) {
            let pos = e.getLocation()
            //cc.log(' onTouchMove pos.x ', pos.x, pos.y)
            this.toyPos[this.curIndex].x = pos.x;
            this.toyPos[this.curIndex].y = pos.y;
        }
    }

    updateToy(index: number) {
        let model: SnowToyModel = this.model.getToyList()[this.curIndex]
        this.model.setUseToy(index, model)
        ModuleManager.loadRes(model.getIcon(), ResType.SpriteFrame, (err, item: ResItem) => {
            if (err || !cc.isValid(this.node)) {
                return;
            }
            this.toySprites[index].spriteFrame = item.getRes();
        })
    }

    onTouchEnd(e: cc.Touch) {
        if (this.curIndex >= 0) {
            // let pos = e.getLocation()
            // pos = this.node.convertToNodeSpaceAR(pos)
            for (let index = 0; index < this.collidePos.length; index++) {
                const element = this.collidePos[index];
                if (CollisionHelper.isRectCollideRect(element.getBoundingBoxToWorld(), this.toyPos[this.curIndex].getBoundingBoxToWorld())) {
                    this.updateToy(index)
                    break;
                }
            }
            this.toyPos[this.curIndex].x = this.initPos.x;
            this.toyPos[this.curIndex].y = this.initPos.y;
            this.curIndex = -1;
        }

    }
    // update (dt) {}

    onButtonBackClick() {
        this.controller.back();
    }

    onButtonNextClick() {
        this.controller.next();
    }
}
